<?php
/**
 * @author zhaofei
 * @package 基础业务控制器 
 * @createtime 2014-11-28
 * @updatetime 2014-12-05
 * @ps:报单信息
 */
namespace System\Controller;
use Think\Controller;
class AmaldarController extends Controller {
    private  $is_send_email;
    private $email;
	function __construct(){
		parent::__construct();
		$ranks = session('ranks');
		if($ranks!=3){
			$this->error('对不起你没有权限',U('Login/index'));
		}
	}


    public function isSend(){
        $rs = D('Config')->field('is_notify_kefu,kefu_email')->find(1);
        if($rs['is_notify_kefu']==1){
            $this->is_send_email = true;
            $this->email = $rs['kefu_email'];
        }else{
            $this->is_send_email = false;
        }        
    }

     

    public function index(){
    	$conditions = array('id'=>session('id'));
    	$data = D('Employee')->where($conditions)->find();
    	$this->assign('data',$data);
        $this->assign('msg',$msg);
    	$this->display();
    }

    //修改密码
    public function edit_password(){
        $this->display();
    }
    //新增报单
    public function add(){
        $stores = $this->getStoreInfo();
        if($stores['store_ids']){
            $this->assign('store_ids',$stores['store_ids']);
        }else{
            $this->error('你还没有关联终端');
        }
        //products
        $data = D('Products_class')->order('sort asc')->where(array('status'=>1))->select();
        $num = count($data);
        for($i=0;$i<$num;$i++) {
            $del = D('Products_class')->where(array('p_id'=>$data[$i]['id']))->find();
            if($del){
                unset($data[$i]);
            }
        }
        $this->assign('data',$data);
        $this->display();
    }


    public function getProductList(){
        $p_id = I('post.p_id');
        $data = array();
        if($p_id){
            $products = D('Products')->where(array('p_id'=>$p_id))->
                order('createtime desc')->select();
            if($products){
                $data = array('status'=>1,'data'=>$products);
            }else{
                $data = array('status'=>0,'data'=>'该类下无产品');
            }
        }else{
            $data = array('status'=>0,'data'=>'参数错误');
        }
        $this->ajaxReturn($data);
    }

 
    public function order(){
        // $cons = array('e_id'=>session('id'));
        $cons = array();
        if(isset($_GET['st'])){
            $cons['examize_status'] = $_GET['st'] ; 
        }
        if(isset($_GET['month'])){
            $month_s = date('Y-m');
            $cons['write_time'] = array('between',array(strtotime($month_s),time()));
        }
        $model = M('Order'); // 实例化
        $count = $model->where($cons)->count();// 查询满足要求的总记录数
          // echo $model->getLastSql();
        // exit();
        $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $model->where($cons)->order('write_time desc')->
            limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }

    public function edit($id){
        $data = D('Order')->where(array('id'=>$id))->find();
        if($data['examize_status']!=0){
            $this->error('当前报单更改期已过');
        }
        if(I('post.')){
            $value = D('Order')->create();
            if($value){
                $rs = D('Order')->where(array('id'=>$id))->save($value);
                 if($rs){
                    $this->success('修改成功',U('order'));
                }else{
                    $this->error('修改失败');
                }
            }else{
                $this->error(D('Order')->getError());
            }
        }else{

            $this->assign('data',$data);
            $this->display('edit');
        }

    }


    public function view($id){
        $data = D('Order')->where(array('id'=>$id))->find();
        // dump($data);
        $this->assign('data',$data);
        $this->display();  
    }

    public function add_staff(){
        $this->display();
    }

     public function staff(){
        $model = M('Employee'); // 实例化Employee对象
        // $cons = array('ct_by_id'=>session('id'));
        $cons = array('ranks'=>array('eq',4));
        $count = $model->where($cons)->count();// 查询满足要求的总记录数
        // echo $model->getLastSql();
        $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $model->where($cons)->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }


     public function addEmployee(){
        $data = D('Employee')->create();
        if($data==false){
            $this->error(D('Employee')->getError());
        }
        if(!I('post.id')){
            $rs = D('Employee')->add($data);
            if($rs){
                $this->success('添加信息成功',U('index'));
            }else{
                $this->error('系统故障');
            }  
        }else{
            $rs = D('Employee')->where(array('id'=>I('post.id')))->save($data);
            if($rs){
                $this->success('修改成功',U('staff'));
            }else{
                $this->error('系统故障');
            }  
        }
    }

    public function edit_staff(){
        if(I('post.id')){
            $data = D('Employee')->create();
            if($data==false){
                $this->error(D('Employee')->getError());
            }else{
                $rs = D('Employee')->where(array('id'=>I('post.id')))->save($data);
                if($rs){
                    $this->success('修改成功',U('staff'));
                }else{
                    $this->error('系统故障');
                } 
            }
        }else{
            if(I('get.id')){
                 $Employee = D('Employee')->where(array('id'=>I('get.id')))->find();
                 $this->assign('Employee',$Employee);
                 $this->display();
            }else{
                $this->error('参数错误');
            }
        }
        
    }

    public function del(){
        $id = I('get.id');
        if(!$id){
            $this->error('参数错误');
        }
        $rs = D('Employee')->delete($id);
        //清除相应终端
        D('EmStore')->where(array('e_id'=>$id))->delete();
        // echo D('EmStore')->getLastSql();
        // exit();
        //清除报单
        D('Order')->where(array('e_id'=>$id))->delete();

        if($rs){
            $this->success('删除成功',U('staff'));
        }else{
            $this->error('系统故障');
        }
    }

    public function getStoreList(){
        $cons = array(
            'provice'=>I('post.province'),
            'city'=>I('post.city'),
            'area'=>I('post.area'),
            'status'=>1
            );
        $data = D('Store')->field('id,name')
        ->where($cons)->order('sort asc')->select();
        // echo D('Store')->getLastSql();
        if($data){
            $this->ajaxReturn(array('status'=>true,'data'=>$data));
        }else{
            $this->ajaxReturn(array('status'=>false,'data'=>'无'));

        }
    }


    //业务终端管理

    public function em_store(){
        $id = I('get.id');
        if(!$id){
            $this->error('参数错误');
        }
        $staff = D('Employee')->find($id);
        $store = D('EmStore')->where(array('e_id'=>$id))->find();
        if($store){
            $this->assign('store',$store);
        }
        $this->assign('staff',$staff);
        $this->display();
    }

     public function em_store_save(){
        $id = I('post.id');
        $data = D('EmStore')->create();
        if($data){
            if($id){
               $rs = D('EmStore')->where(array('id'=>$id))->save($data);
               $msg = '修改成功';
            }else{
               $rs = D('EmStore')->add($data);
               $msg = '添加成功';
            }
            if($rs){
                $this->success($msg);
            }else{
                $this->error('系统错误');
            }
        }else{
            $this->error(D('EmStore')->getError());
            exit();
        }
    }


    //修改密码
    public function password(){
        if(I('post.password')){
            $pass = md5(md5(I('post.password'),true));
            $result = D('Employee')->where(array('id'=>session('id')))->save(array('password'=>$pass));
            if($result){
                $this->success('修改成功，当前用户自动注销',U('Login/logout'));
            }else{
                $this->error('修改失败');
            }
        }else{
            $this->error('密码不能为空');
        }
    }
 


    
    



}
